package com.jms.config;

import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * create by yongli on 2021-06-05 13:54
 */
@Configuration
public class RemoteDataSourceConfig {
    public static final Logger logger = LoggerFactory.getLogger(RemoteDataSourceConfig.class);

    @Value("${spring.datasource.remoteData.url}")
    private String url;
    @Value("${spring.datasource.remoteData.username}")
    private String username;
    @Value("${spring.datasource.remoteData.password}")
    private String password;
    @Value("${spring.datasource.remoteData.driverClassName}")
    private String driverClassName;


    // 生成一个dataSource对象
    @Bean(name = "remoteDataSource")
    public DataSource getRemoteDataSource() {

        PooledDataSource dataSource = new PooledDataSource(driverClassName, url, username, password);

        // dataSource是由为外部实现的
        logger.info(dataSource.getPassword());
        logger.info(dataSource.getUrl());
        return dataSource;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getDriverClassName() {
        return driverClassName;
    }

    public void setDriverClassName(String driverClassName) {
        this.driverClassName = driverClassName;
    }
}
